package com.cmc6.common.config;

import cn.hutool.system.SystemUtil;
import com.cmc6.common.properties.Cmc6AdminProperties;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 主要的 config类
 */
@Configuration
@ComponentScan(basePackages = "com.cmc6")
@MapperScan(basePackages = "com.cmc6.**.mapper")
@ServletComponentScan
@Order(value = Integer.MIN_VALUE)
public class BaseConfig {

    public static String applicationName; // 服务名
    public static boolean adminEnable; // 是否允许 admin登录，默认为 false
    public static String adminUsername; // admin的用户名，默认为 admin
    public static String adminPassword; // admin的密码，默认为 suancai
    public static String socketAddress; // 返回socket 连接地址时，使用的 ip
    public static Date startTime = new Date(); // 启动耗时
    public static String address = SystemUtil.getHostInfo().getAddress(); // 当前主机的地址
    public static Integer port; // 启动的端口
    public static Cmc6AdminProperties cmc6AdminProperties;
    public static boolean prodFlag; // 是否是正式环境

    @Value("${spring.application.name:cmc6-admin}")
    private void setApplicationName(String value) {
        BaseConfig.applicationName = value;
    }

    @Resource
    private void setCmc6AdminProperties(Cmc6AdminProperties val) {
        BaseConfig.cmc6AdminProperties = val;
        BaseConfig.adminEnable = val.isAdminEnable();
        BaseConfig.adminUsername = val.getAdminUsername();
        BaseConfig.adminPassword = val.getAdminPassword();
        BaseConfig.socketAddress = val.getSocketAddress();
    }

    @Value("${server.port:8080}")
    private void setPort(Integer value) {
        BaseConfig.port = value;
    }

    @Value("${spring.profiles.active:prod}")
    private void setProdFlag(String value) {
        BaseConfig.prodFlag = "prod".equals(value);
    }

}
